package com.wbao.dianniu.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class JsonUtil {
    private static GsonBuilder gsonBuilder = new GsonBuilder();
    private static Gson gson = getJson();
    private static Gson booleanGson = getBooleanJson();


    public static <T> T toObject(String json, Class<T> obj) {
        T t = null;
        try {
            t = gson.fromJson(json, obj);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return t;
    }

    public static <T> T toObject(Object jsonObj, Class<T> obj) {
        T t = null;
        try {
            t = gson.fromJson((JsonElement) jsonObj, obj);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return t;
    }

    public static <T> T toBooleanObject(Object jsonObj, Class<T> obj) {
        T t = null;
        try {
            t = gson.fromJson(booleanGson.toJson(jsonObj), obj);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return t;
    }

    public static <T> List<T> toObjectList(String json, Class<T> cls) {
        List<T> list = new ArrayList<T>();
        try {
            JsonArray arry = new JsonParser().parse(json).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static <T> List<T> toObjectList(Object jsonObj, Class<T> cls) {
        List<T> list = new ArrayList<T>();
        try {
            JsonArray arry = new JsonParser().parse(gson.toJson(jsonObj)).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static Gson getJson() {
        gsonBuilder.registerTypeAdapter(Double.class, new JsonSerializer<Double>() {

            public JsonElement serialize(Double src, Type typeOfSrc,
                                         JsonSerializationContext context) {
                return new JsonPrimitive(Long.valueOf(src.longValue()).toString());
            }

        });
        Gson gs = gsonBuilder.create();
        return gs;
    }

    public static Gson getBooleanJson() {
        gsonBuilder.registerTypeAdapter(Boolean.class, new JsonSerializer<Boolean>() {

            public JsonElement serialize(Boolean src, Type typeOfSrc,
                                         JsonSerializationContext context) {
                boolean sr = src.booleanValue();
                return new JsonPrimitive(Boolean.valueOf(sr).toString());
            }

        });
        Gson gs = gsonBuilder.create();
        return gs;
    }

    /**
     * map转Json
     *
     * @param noticeJson
     * @return
     */
    public static String mapToStringJson(Map<Integer, String> noticeJson) {
        if (null == noticeJson) {
            return null;
        }
        JsonObject jsonObject = new JsonObject();
        for (Map.Entry<Integer, String> entry : noticeJson.entrySet()) {
            jsonObject.addProperty(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
        }
        return jsonObject.toString();
    }

}
